package com.zxs.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zxs.common.util.PageResult;
import com.zxs.entity.Classes;
import com.zxs.entity.Student;
import com.zxs.mapper.StudentMapper;
import com.zxs.service.IClassesService;
import com.zxs.service.IStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxs.vo.StudentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zxs
 * @since 2025-01-07
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {

    @Autowired
    private IClassesService classesService;
        @Override
        public PageResult<Student> findByPage(Integer page, Integer pageSize) {
            // 1. 开始分页
            Page<Student> page1 = this.page(new Page<>(page, pageSize));

            // 2. 封装分页结果
            return new PageResult<>(page1.getRecords(),page1.getTotal());
        }

    @Override
    public PageResult<Student> search(Integer page, Integer pageSize, StudentVO vo) {

        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(vo.getSname()), "sname", vo.getSname());
        wrapper.like(StrUtil.isNotBlank(vo.getAddr()), "addr", vo.getAddr());
        wrapper.eq(vo.getCid() != null && vo.getCid() != 0, "cid", vo.getCid());
        Page<Student> page1 = this.page(new Page<>(page, pageSize), wrapper);
        page1.getRecords().forEach(st -> {
            Classes classes = classesService.getById(st.getCid());
            st.setCname(classes.getCname());
        });
        return new PageResult<>(page1.getRecords(),page1.getTotal());
    }
}